#include "InetAddress.h"

InetAddress::InetAddress(const std::string &addr, uint16_t port)
{
    addr_.sin_family = AF_INET;
    addr_.sin_port = htons(port);
    if (!::inet_pton(AF_INET, addr.c_str(), &addr_.sin_addr))
    {
        perror("inet_pton");
        assert(false);
    }
}

uint16_t InetAddress::port()
{
    return ntohl(addr_.sin_port);
}

std::string InetAddress::ip()
{
    char buf[64];
    if(!::inet_ntop(AF_INET,&addr_.sin_addr,buf,sizeof(buf))){
        perror("inet_ntop");
        assert(false);
    }
    return std::string(buf);
}
